package com.bd.scm.module.expend.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.scm.domain.dto.aggrid.AgGridRequest;
import com.bd.scm.domain.dto.aggrid.AgGridResponse;
import com.bd.scm.domain.dto.expend.QywxConsumeRecordDto;
import com.bd.scm.domain.models.expend.QywxConsumeRecord;
import com.bd.scm.module.common.BaseController;
import com.bd.scm.module.expend.service.QywxConsumeRecordService;
import com.bd.utils.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**企业微信 消费记录 Controller
 * @ClassName QywxConsumeRecordController
 * @Description TODO
 * @Author YH
 * @Date 2020/3/25 0025 14:33
 * @Version 1.0
 **/
@RestController
@RequestMapping("/qywx")
public class QywxConsumeRecordController extends BaseController {

  private Logger log = LoggerFactory.getLogger("qywxDataLogger");

  @Autowired
  private QywxConsumeRecordService qywxConsumeRecordService;

  /**
   * 获取个人消费列表
   * @param httpServletRequest 人员ID userSerial 开始日期 startDate 结束日期 endDate
   * @param request
   * @return
   */
  @PostMapping("getPersonalConsumeList")
  public Result<AgGridResponse> getPersonalConsumeList(HttpServletRequest httpServletRequest, @RequestBody
      AgGridRequest request) {
    log.info("params: {}", JSON.toJSON(request));
    Result<AgGridResponse> result = new Result<AgGridResponse>();

    Page<QywxConsumeRecord> page = getPage(request);

    //之后要根据传值，
    QywxConsumeRecordDto qywxConsumeRecordDto = JSON.parseObject(JSON.toJSONString(request.getData()),
        QywxConsumeRecordDto.class);
    //获取登录人ID
//    JSONObject user = (JSONObject) httpServletRequest.getSession().getAttribute("user");
//    qywxConsumeRecordDto.setUserSerial(user.getString("userSerial"));
    IPage<QywxConsumeRecord> pageList = qywxConsumeRecordService.getPersonalConsumeList(page,qywxConsumeRecordDto);
    AgGridResponse<QywxConsumeRecord> response = getResponse(request, pageList, page);
    log.info("totalPages: {}", pageList.getPages());
    result.setSuccess(true);
    result.setResult(response);
    return result;
  }

  /**
   * 获取个人充值列表
   * @param httpServletRequest 人员ID userSerial 开始日期 startDate 结束日期 endDate
   * @param request
   * @return
   */
  @PostMapping("getPersonalRechargeList")
  public Result<AgGridResponse> getPersonalRechargeList(HttpServletRequest httpServletRequest, @RequestBody
      AgGridRequest request) {
    log.info("params: {}", JSON.toJSON(request));
    Result<AgGridResponse> result = new Result<AgGridResponse>();

    Page<QywxConsumeRecord> page = getPage(request);

    //之后要根据传值，
    QywxConsumeRecordDto qywxConsumeRecordDto = JSON.parseObject(JSON.toJSONString(request.getData()),
        QywxConsumeRecordDto.class);
    //获取登录人ID
//    JSONObject user = (JSONObject) httpServletRequest.getSession().getAttribute("user");
//    qywxConsumeRecordDto.setUserSerial(user.getString("userSerial"));
    IPage<QywxConsumeRecord> pageList = qywxConsumeRecordService.getPersonalRechargeList(page,qywxConsumeRecordDto);
    AgGridResponse<QywxConsumeRecord> response = getResponse(request, pageList, page);
    log.info("totalPages: {}", pageList.getPages());
    result.setSuccess(true);
    result.setResult(response);
    return result;
  }


  /**
   * 获取个人补贴列表
   * @param httpServletRequest 人员ID userSerial 开始日期 startDate 结束日期 endDate
   * @param request
   * @return
   */
  @PostMapping("getPersonalSubsidyList")
  public Result<AgGridResponse> getPersonalSubsidyList(HttpServletRequest httpServletRequest, @RequestBody
      AgGridRequest request) {
    log.info("params: {}", JSON.toJSON(request));
    Result<AgGridResponse> result = new Result<AgGridResponse>();

    Page<QywxConsumeRecord> page = getPage(request);

    //之后要根据传值，
    QywxConsumeRecordDto qywxConsumeRecordDto = JSON.parseObject(JSON.toJSONString(request.getData()),
        QywxConsumeRecordDto.class);
    //获取登录人ID
//    JSONObject user = (JSONObject) httpServletRequest.getSession().getAttribute("user");
//    qywxConsumeRecordDto.setUserSerial(user.getString("userSerial"));
    IPage<QywxConsumeRecord> pageList = qywxConsumeRecordService.getPersonalSubsidyList(page,qywxConsumeRecordDto);
    AgGridResponse<QywxConsumeRecord> response = getResponse(request, pageList, page);
    log.info("totalPages: {}", pageList.getPages());
    result.setSuccess(true);
    result.setResult(response);
    return result;
  }
}
